package com.msc.cili.service.guestmessage.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msc.cili.common.utils.DateUtil;
import com.msc.cili.repository.dao.guestmessage.GuestMessageMapper;
import com.msc.cili.repository.entity.guestmessage.GuestMessage;
import com.msc.cili.service.guestmessage.GuestMessageService;
import com.msc.cili.service.guestmessage.bo.GuestMsgBo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author masc
 * @date 2022-07-17 1:11
 * <p></p>
 */
@Service
public class GuestMessageServiceImpl extends ServiceImpl<GuestMessageMapper, GuestMessage> implements GuestMessageService {
    @Autowired
    private GuestMessageMapper guestMessageMapper;
//    @Autowired
//    UserAgentUtils userAgentUtils;

    @Override
    public IPage<GuestMessage> getMsgListByPage(int pageIndex, int pageSize) {
        QueryWrapper<GuestMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(GuestMessage::getStatus, 1);
        queryWrapper.lambda().orderByDesc(GuestMessage::getCreateTime); //根据时间倒序
        Page page = new Page(pageIndex, pageSize);
        IPage<GuestMessage> pageList = this.page(page, queryWrapper);

        List<GuestMessage> collect = pageList.getRecords().stream().map(v -> {
            v.setCtTime(DateUtil.dateToString(v.getCreateTime(), DateUtil.DATE_FORMAT_DATETIME2));
            return v;
        }).collect(Collectors.toList());

        pageList.setRecords(collect);

        return pageList;
    }

    @Override
    public boolean createMsg(String ip, GuestMsgBo guestMsgBo) {
        GuestMessage guestMessage = new GuestMessage();
        BeanUtils.copyProperties(guestMsgBo, guestMessage);
        guestMessage.setCreateTime(LocalDateTime.now());
        guestMessage.setUpdateTime(LocalDateTime.now());
        guestMessage.setStatus(1);

        //设置访问信息
        //获取用户IP
//        String ip = request.getHeader("x-forwarded-for");
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("WL-Proxy-Client-IP");
//        }
//        //以上两种方式都获取不到，那就使用前端传过来的
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            // 前端通过搜狐API获取ip放到header里面
//            ip = request.getHeader("yourIp");
//        }
////        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
////            ip = request.getRemoteAddr();
////        }
//        if (null == ip) {
//            //如果Ip为空，不让进行操作
//            throw new CiliciliException(CiliciliErrorCodeEnum.NOT_IP);
//        }
//        String ipSource = IpAddressUtils.getCityInfo(ip);
//        String userAgent = request.getHeader("User-Agent");
//        Map<String, String> userAgentMap = userAgentUtils.parseOsAndBrowser(userAgent);
//        String os = userAgentMap.get("os");
//        String browser = userAgentMap.get("browser");
//        guestMessage.setIp(ip);
//        guestMessage.setIpSource(ipSource);
//        guestMessage.setOs(os);
//        guestMessage.setBrowser(browser);
//        //判断当前ip下，今日留言数量是否大于2，大于不让操作
//        QueryWrapper<GuestMessage> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(GuestMessage::getIp, ip)
//                .ge(GuestMessage::getCreateTime, DateUtil.getDateStart()) //大于今天的00:00
//                .le(GuestMessage::getCreateTime, DateUtil.getDateEnd());   //小于今天的23：59
//
//        int count = this.count(queryWrapper);
//        if (count > 2) {
//            return false;
//        }
        int insertNums = guestMessageMapper.insert(guestMessage);
        return insertNums > 0;
    }

}
